package game.Player;


import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;

public class Input{
    private static boolean[] currentKeys = new boolean[128];
    private static boolean[] oldKeys = new boolean[128];
    private static boolean[] currentKeysClicked = new boolean[128];

    private static boolean mouse = false;
    private static boolean oldMouse = false;
    private static boolean mouseClicked = false;

    public static boolean getKey(KeyCode keyCode) {
        return currentKeys[keyCode.ordinal()];
    }
    public static boolean getKeyClick(KeyCode keyCode) {
        return currentKeysClicked[keyCode.ordinal()];
    }
    public static boolean getMouse() {
        return mouse;
    }
    public static boolean getMouseClick() {
        return mouseClicked;
    }

    public static void update(){
        if(mouse && !oldMouse){
            mouseClicked = true;
        }else{
            mouseClicked = false;
        }
        oldMouse = mouse;

        for(int i = 0; i < currentKeys.length; i++){
            if(currentKeys[i] && !oldKeys[i]){
                currentKeysClicked[i] = true;
            }else{
                currentKeysClicked[i] = false;
            }
            oldKeys[i] = currentKeys[i];

        }

    }

    public void keyTyped(KeyEvent e ) {

    }
    public void keyPressed(KeyEvent e) {
        if(e.getCode().ordinal() >= 0 && e.getCode().ordinal() < currentKeys.length)
            currentKeys[e.getCode().ordinal()] = true;
    }
    public void keyReleased(KeyEvent e) {
        if(e.getCode().ordinal() >= 0 && e.getCode().ordinal() < currentKeys.length)
            currentKeys[e.getCode().ordinal()] = false;
    }


    // MouseListener
    public void mouseClicked(MouseEvent mouseEvent) {
        // Da fallos (salta despues de mousereleased)
    }
    public void mousePressed(MouseEvent mouseEvent) {
        mouse = true;
    }

    public void mouseReleased(MouseEvent mouseEvent) {
        mouse = false;
    }

    public void mouseEntered(MouseEvent mouseEvent) {
    }

    public void mouseExited(MouseEvent mouseEvent) {
        mouse = false;
    }

}
